qtre.sceneManager = {

	_cameras : [],

	_lights : [],
	
	_sceneNodes : [],

	_sceneNodeIndex : 0,

	_renderables : [],

	_renderableIndex : 0,

	_materials : [],

	_materialIndex : 0,

	elapsedTime : 0,

	now : 0,

	time : 0,

	addCamera : function(camera){
		this._cameras[camera.name] = camera;
	},
	
	createCamera : function(name, opt){
		if(qtre.core.isset(opt.fov)){	//perspective
			var camera = new qtre.cameraPerspective(opt.eye, opt.target, opt.up, opt.fov, opt.near, opt.far);
		}
		else{		//orgho
			var camera = new qtre.cameraOrtho(opt.eye, opt.target, opt.up, opt.left, opt.right, opt.bottom, opt.up, opt.near, opt.far);
		}
		camera.name = name;
		this._cameras[name] = camera;
		return camera;
	},

	getCamera : function(name){
		return this._cameras[name];
	},

	getCameras : function(){
		return this._cameras;
	},

	flush : function(){
		//update frame time
		var now = (new Date()).getTime() * 0.001;
		if(!this.now){
			this.now = now;
		}else{
			this.elapsedTime = now - this.now;
			this.now = now;
		}
		this.time += this.elapsedTime;
		//transform the sceneNodes
		for(var name in this._sceneNodes){
			this._sceneNodes[name].onRenderBegin();
		}

		for(var name in this._cameras){
			var camera = this._cameras[name];
			camera.onRenderBegin();
			camera.render();
			camera.onRenderEnd();
		}
	},

	createSceneNode : function(name){
		var name = name || "default_"+this._sceneNodeIndex;
		this._sceneNodes[name] = new qtre.sceneNode(name);
		this._sceneNodeIndex++;
		return this._sceneNodes[name];
	},

	getSceneNode : function(name){
		return this._sceneNodes[name];
	},

	getSceneNodes : function(){
		return this._sceneNodes;
	},

	createRenderable : function(name, arrays){
		var name = name || "default_"+this._renderableIndex;
		this._renderables[name] = new qtre.renderable(name, arrays);
		this._renderableIndex++;
		return this._renderables[name];
	},

	getRenderable : function(name){
		return this._renderables[name];
	},

	getRenderables : function(){
		return this._renderables;
	},

	createMaterial : function(name, program){
		var name = name || "default_"+this._materialIndex;
		//check the if it is a common material like phong, lambert
		var material;
		switch(program){
			case "phong":
				material = new qtre.commonMaterials.phong(name);
				break;
			default:
				material = new qtre.material(name, program);
				break;
		}
		this._materials[name] = material;
		this._materialIndex++;
		return this._materials[name];
	},

	getMaterial : function(name){
		return this._materials[name];
	}
	
	
}